package com.lzy.concertticket.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzy.concertticket.dao.UserDao;
import com.lzy.concertticket.model.entity.UserEntity;
import com.lzy.concertticket.model.req.UserReq;
import com.lzy.concertticket.model.vo.UserVo;
import com.lzy.concertticket.service.UserService;
import com.lzy.concertticket.threadlocal.ResponseThreadLocal;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.List;

/**
* @author 30793
* @description 针对表【tb_user】的数据库操作Service实现
* @createDate 2025-08-15 22:50:26
*/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    @Override
    public List<UserEntity> list() {
        return userDao.list();
    }

    @Override
    public UserEntity getUserByAccount(String account) {
        return userDao.getUserByAccount(account);
    }

    @Override
    public Page<UserEntity> getUserPage(UserReq userReq) {

        return userDao.getUserPage(userReq);
    }

    @Override
    public void removeById(Long userId) {
        userDao.removeById(userId);
    }

    @Override
    public void updateById(UserEntity userEntity) {
        userDao.updateById(userEntity);
    }

    @Override
    public UserVo getInfo(UserReq userReq) {
        UserEntity userEntity = userDao.getInfo(userReq);
        UserVo userVo = BeanUtil.copyProperties(userEntity, UserVo.class);
        return userVo;
    }

    @SneakyThrows
    @Override
    public void export() {

        List<UserEntity> userList = userDao.list();
        HttpServletResponse response = ResponseThreadLocal.get();
        //  设置响应文件为excel
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=user_data.xlsx");
        ServletOutputStream outputStream = response.getOutputStream();
        EasyExcel.write(outputStream, UserEntity.class).sheet().doWrite(userList);
        ResponseThreadLocal.remove();
    }
}




